var gulp = require("gulp");
var	imagemin = require('gulp-imagemin');              //压缩图片1
var	tinypng = require('gulp-tinypng-compress');       //压缩图片2 需要有KEY,下面有将怎样获取KEY值
var tinypng_nokey = require('gulp-tinypng-nokey');    //压缩图片3 免费
// var runSequence = require('run-sequence');


// //图片压缩1 (感觉压缩程度不够)
// gulp.task('compress_img', function () {
//     gulp.src('redbaby/public/images/redbaby/*.{png,jpg,jpeg,gif,ico}')
//         .pipe(imagemin({
//             optimizationLevel: 5, //类型：Number  默认：3  取值范围：0-7（优化等级）
//             progressive: true,    //类型：Boolean 默认：false 无损压缩jpg图片
//             interlaced: true,     //类型：Boolean 默认：false 隔行扫描gif进行渲染
//             multipass: true       //类型：Boolean 默认：false 多次优化svg直到完全优化
//         }))
//         .pipe(gulp.dest('gulptest/yes/img'))
// });

// //压缩图片3 (免费 常用)
// gulp.task('tp', function() {
//     gulp.src('redbaby/public/images/redbaby/*.{png,jpg,jpeg,gif,ico}')
//         .pipe(tinypng_nokey ())
//         .pipe(gulp.dest('gulptest/yes/img'));
// })

// gulp.task('build', function (done) {
//     condition = false;
//     runSequence(
//         'compress_img',
//         'tinypng',
//         'tp',

//     done);
// });

gulp.task('compress_img', (cb)=>{
    gulp.src('../Git/Gpl-Own/Chivalrous_man_city_newstruct/build/web-mobile/**/*.{png,jpg,jpeg}')
        .pipe(imagemin([
            imagemin.gifsicle({interlaced: true}),
            imagemin.jpegtran({progressive: true}),
            imagemin.optipng({optimizationLevel: 5})
        ]))
        .pipe(gulp.dest("../Git/Gpl-Own/Chivalrous_man_city_newstruct/build/web-mobile/"))
        .on("end", cb);
})


//压缩图片 (需要有KEY,并且每个月只有500张)
gulp.task('tinypngsig', (cb)=> {
    gulp.src('../Git/Gpl-Own/Chivalrous_man_city_newstruct/build/web-mobile/**/*.{png,jpg,jpeg}')
        .pipe(
            tinypng({
            key: 'Gi7dwKoGcu1rgTJnUuTTh3bYDZpFaFp8',
            sigFile: '../Git/Gpl-Own/Chivalrous_man_city_newstruct/build/.tinypng-sigs',
            log: true
            })
        ).on('error', (err)=>{
            console.log('err=>',err.message);
        })
        .pipe(gulp.dest("../Git/Gpl-Own/Chivalrous_man_city_newstruct/build/web-mobile/"))
        .on("end", cb);
});
 

